
from array import *

def str2hex(str):
  xx = bytearray()
  temp = ""
  for ch in str:
    temp += ch
    if len(temp) == 2:
      #print "0x"+temp
      xx.append(int("0x"+temp,16))
      temp = ""
  return xx

  
def hex2str(hex):
  s = ""
  temp = ""
  for ch in hex:
    i = ord(ch)
    s += ch.encode("hex")
  return s

toDataModeStr = "020FFE117777772E6E6574766F782E636F6D2E636ECB"
toDataMode = str2hex(toDataModeStr)
print "Str1:",toDataMode
toCommandModeStr = "020FFE115757572E4E4554564F582E434F4D2E434ECB"
toCommandMode = str2hex(toCommandModeStr)
print "Str2:",toCommandMode
print "-------------------------"


#exit(0)

print "Begin"
import serial

ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=1)
#ser = serial.Serial('/dev/ttyUSB0')  # open first serial port
print ser.portstr       # check which port was really used

print "Going to data ..."
ser.write(toDataMode)
line = ser.readline()
print "len",len(line)
decoded = hex2str(line)
print decoded


print "Going to command ..."
ser.write(toCommandMode)
line = ser.readline()
print "len",len(line)
decoded = hex2str(line)
print decoded

ser.close()             # close port
